﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Public
{
    public class TaiKhoan_PB
    {
        private static string _TenDangNhap;

        public static string TenDangNhap
        {
            get { return TaiKhoan_PB._TenDangNhap; }
            set { TaiKhoan_PB._TenDangNhap = value; }
        }
        private static string _TenTaiKhoan;

        public static string TenTaiKhoan
        {
            get { return TaiKhoan_PB._TenTaiKhoan; }
            set { TaiKhoan_PB._TenTaiKhoan = value; }
        }
        private static string _MatKhau;

        public static string MatKhau
        {
            get { return TaiKhoan_PB._MatKhau; }
            set { TaiKhoan_PB._MatKhau = value; }
        }

        private static string _MatKhauMoi;

        public static string MatKhauMoi
        {
            get { return TaiKhoan_PB._MatKhauMoi; }
            set { TaiKhoan_PB._MatKhauMoi = value; }
        }
        private static string _HoTen;

        public static string HoTen
        {
            get { return TaiKhoan_PB._HoTen; }
            set { TaiKhoan_PB._HoTen = value; }
        }
        private static string _DonVi;

        public static string DonVi
        {
            get { return TaiKhoan_PB._DonVi; }
            set { TaiKhoan_PB._DonVi = value; }
        }
        private static string _Email;

        public static string Email
        {
            get { return TaiKhoan_PB._Email; }
            set { TaiKhoan_PB._Email = value; }
        }

        private byte[] _HinhAnh;

        public byte[] HinhAnh
        {
            get { return _HinhAnh; }
            set { _HinhAnh = value; }
        }

        #region Mã hóa mật khẩu
        public string Encrypt(string toEncrypt, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);
            if (useHashing)
            {
                var hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(Encoding.UTF8.GetBytes("Phanmemquanlykho.com.vn"));
            }
            else keyArray = Encoding.UTF8.GetBytes("Phanmemquanlykho.com.vn");
            var tdes = new TripleDESCryptoServiceProvider
            {
                Key = keyArray,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };
            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        #endregion

        #region Giải mã mật khẩu
        public string Decrypt(string toDecrypt, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
            if (useHashing)
            {
                var hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(Encoding.UTF8.GetBytes("Phanmemquanlykho.com.vn"));
            }
            else keyArray = Encoding.UTF8.GetBytes("Phanmemquanlykho.com.vn");
            var tdes = new TripleDESCryptoServiceProvider
            {
                Key = keyArray,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };
            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Encoding.UTF8.GetString(resultArray);
        }
        #endregion
    }
}
